﻿using System;
using System.Collections.Generic;
using System.Text;

namespace XKPlatform.Common
{
    public class WeixinAccessTokenHelper
    {
        //accesstoken 上次获取时间
        public static DateTime LastRequestTime = DateTime.Now;

        //accesstoken 有效期
        public static int ExpireTime = 0;

        //记录上次获取token
        public static string AccessToken = null;

        /// <summary>
        /// 判断是否需要重新获取AccessToken
        /// </summary>
        /// <returns></returns>
        public static bool GetTokenPermit() {
            //默认需要重新请求token
            bool result = true;
            try {
                //初次获取
                if (ExpireTime <= 0)
                {
                    result = true;
                }
                else
                {
                    TimeSpan st = new TimeSpan(DateTime.Now.Ticks);
                    TimeSpan ed = new TimeSpan(LastRequestTime.Ticks);
                    TimeSpan ts = st.Subtract(ed).Duration();
                    if (int.Parse(ts.Seconds.ToString()) < ExpireTime)
                    {
                        result = false;
                    }
                }

            }
            catch (Exception ex) {
                LogHelper.SaveLog(ex);
                result = true;
            }
            return result;
        }
    }
}
